﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using Indeco.G24.Entities;
using Indeco.G24.Data;

#endregion

namespace Indeco.G24.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="NganhNgheKinhDoanhProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class NganhNgheKinhDoanhProviderBaseCore : EntityProviderBase<Indeco.G24.Entities.NganhNgheKinhDoanh, Indeco.G24.Entities.NganhNgheKinhDoanhKey>
	{		
		#region Get from Many To Many Relationship Functions
		#region GetByIDNhaCungCapFromNhaCungCapNganhNgheKinhDoanh
		
		/// <summary>
		///		Gets tb_NganhNgheKinhDoanh objects from the datasource by ID_NhaCungCap in the
		///		tb_NhaCungCapNganhNgheKinhDoanh table. Table tb_NganhNgheKinhDoanh is related to table tb_NhaCungCap
		///		through the (M:N) relationship defined in the tb_NhaCungCapNganhNgheKinhDoanh table.
		/// </summary>
		/// <param name="iDNhaCungCap"></param>
		/// <returns>Returns a typed collection of NganhNgheKinhDoanh objects.</returns>
		public TList<NganhNgheKinhDoanh> GetByIDNhaCungCapFromNhaCungCapNganhNgheKinhDoanh(System.Guid iDNhaCungCap)
		{
			int count = -1;
			return GetByIDNhaCungCapFromNhaCungCapNganhNgheKinhDoanh(null,iDNhaCungCap, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets Indeco.G24.Entities.NganhNgheKinhDoanh objects from the datasource by ID_NhaCungCap in the
		///		tb_NhaCungCapNganhNgheKinhDoanh table. Table tb_NganhNgheKinhDoanh is related to table tb_NhaCungCap
		///		through the (M:N) relationship defined in the tb_NhaCungCapNganhNgheKinhDoanh table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="iDNhaCungCap"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of NganhNgheKinhDoanh objects.</returns>
		public TList<NganhNgheKinhDoanh> GetByIDNhaCungCapFromNhaCungCapNganhNgheKinhDoanh(System.Guid iDNhaCungCap, int start, int pageLength)
		{
			int count = -1;
			return GetByIDNhaCungCapFromNhaCungCapNganhNgheKinhDoanh(null, iDNhaCungCap, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets NganhNgheKinhDoanh objects from the datasource by ID_NhaCungCap in the
		///		tb_NhaCungCapNganhNgheKinhDoanh table. Table tb_NganhNgheKinhDoanh is related to table tb_NhaCungCap
		///		through the (M:N) relationship defined in the tb_NhaCungCapNganhNgheKinhDoanh table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="iDNhaCungCap"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of tb_NganhNgheKinhDoanh objects.</returns>
		public TList<NganhNgheKinhDoanh> GetByIDNhaCungCapFromNhaCungCapNganhNgheKinhDoanh(TransactionManager transactionManager, System.Guid iDNhaCungCap)
		{
			int count = -1;
			return GetByIDNhaCungCapFromNhaCungCapNganhNgheKinhDoanh(transactionManager, iDNhaCungCap, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets NganhNgheKinhDoanh objects from the datasource by ID_NhaCungCap in the
		///		tb_NhaCungCapNganhNgheKinhDoanh table. Table tb_NganhNgheKinhDoanh is related to table tb_NhaCungCap
		///		through the (M:N) relationship defined in the tb_NhaCungCapNganhNgheKinhDoanh table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="iDNhaCungCap"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of tb_NganhNgheKinhDoanh objects.</returns>
		public TList<NganhNgheKinhDoanh> GetByIDNhaCungCapFromNhaCungCapNganhNgheKinhDoanh(TransactionManager transactionManager, System.Guid iDNhaCungCap,int start, int pageLength)
		{
			int count = -1;
			return GetByIDNhaCungCapFromNhaCungCapNganhNgheKinhDoanh(transactionManager, iDNhaCungCap, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets NganhNgheKinhDoanh objects from the datasource by ID_NhaCungCap in the
		///		tb_NhaCungCapNganhNgheKinhDoanh table. Table tb_NganhNgheKinhDoanh is related to table tb_NhaCungCap
		///		through the (M:N) relationship defined in the tb_NhaCungCapNganhNgheKinhDoanh table.
		/// </summary>
		/// <param name="iDNhaCungCap"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of tb_NganhNgheKinhDoanh objects.</returns>
		public TList<NganhNgheKinhDoanh> GetByIDNhaCungCapFromNhaCungCapNganhNgheKinhDoanh(System.Guid iDNhaCungCap,int start, int pageLength, out int count)
		{
			
			return GetByIDNhaCungCapFromNhaCungCapNganhNgheKinhDoanh(null, iDNhaCungCap, start, pageLength, out count);
		}


		/// <summary>
		///		Gets tb_NganhNgheKinhDoanh objects from the datasource by ID_NhaCungCap in the
		///		tb_NhaCungCapNganhNgheKinhDoanh table. Table tb_NganhNgheKinhDoanh is related to table tb_NhaCungCap
		///		through the (M:N) relationship defined in the tb_NhaCungCapNganhNgheKinhDoanh table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="iDNhaCungCap"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of NganhNgheKinhDoanh objects.</returns>
		public abstract TList<NganhNgheKinhDoanh> GetByIDNhaCungCapFromNhaCungCapNganhNgheKinhDoanh(TransactionManager transactionManager,System.Guid iDNhaCungCap, int start, int pageLength, out int count);
		
		#endregion GetByIDNhaCungCapFromNhaCungCapNganhNgheKinhDoanh
		
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, Indeco.G24.Entities.NganhNgheKinhDoanhKey key)
		{
			return Delete(transactionManager, key.IDNganhNgheKinhDoanh);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="iDNganhNgheKinhDoanh">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Guid iDNganhNgheKinhDoanh)
		{
			return Delete(null, iDNganhNgheKinhDoanh);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="iDNganhNgheKinhDoanh">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Guid iDNganhNgheKinhDoanh);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override Indeco.G24.Entities.NganhNgheKinhDoanh Get(TransactionManager transactionManager, Indeco.G24.Entities.NganhNgheKinhDoanhKey key, int start, int pageLength)
		{
			return GetByIDNganhNgheKinhDoanh(transactionManager, key.IDNganhNgheKinhDoanh, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_tb_NganhNgheKinhDoanh index.
		/// </summary>
		/// <param name="iDNganhNgheKinhDoanh"></param>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.NganhNgheKinhDoanh"/> class.</returns>
		public Indeco.G24.Entities.NganhNgheKinhDoanh GetByIDNganhNgheKinhDoanh(System.Guid iDNganhNgheKinhDoanh)
		{
			int count = -1;
			return GetByIDNganhNgheKinhDoanh(null,iDNganhNgheKinhDoanh, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tb_NganhNgheKinhDoanh index.
		/// </summary>
		/// <param name="iDNganhNgheKinhDoanh"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.NganhNgheKinhDoanh"/> class.</returns>
		public Indeco.G24.Entities.NganhNgheKinhDoanh GetByIDNganhNgheKinhDoanh(System.Guid iDNganhNgheKinhDoanh, int start, int pageLength)
		{
			int count = -1;
			return GetByIDNganhNgheKinhDoanh(null, iDNganhNgheKinhDoanh, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tb_NganhNgheKinhDoanh index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="iDNganhNgheKinhDoanh"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.NganhNgheKinhDoanh"/> class.</returns>
		public Indeco.G24.Entities.NganhNgheKinhDoanh GetByIDNganhNgheKinhDoanh(TransactionManager transactionManager, System.Guid iDNganhNgheKinhDoanh)
		{
			int count = -1;
			return GetByIDNganhNgheKinhDoanh(transactionManager, iDNganhNgheKinhDoanh, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tb_NganhNgheKinhDoanh index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="iDNganhNgheKinhDoanh"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.NganhNgheKinhDoanh"/> class.</returns>
		public Indeco.G24.Entities.NganhNgheKinhDoanh GetByIDNganhNgheKinhDoanh(TransactionManager transactionManager, System.Guid iDNganhNgheKinhDoanh, int start, int pageLength)
		{
			int count = -1;
			return GetByIDNganhNgheKinhDoanh(transactionManager, iDNganhNgheKinhDoanh, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tb_NganhNgheKinhDoanh index.
		/// </summary>
		/// <param name="iDNganhNgheKinhDoanh"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.NganhNgheKinhDoanh"/> class.</returns>
		public Indeco.G24.Entities.NganhNgheKinhDoanh GetByIDNganhNgheKinhDoanh(System.Guid iDNganhNgheKinhDoanh, int start, int pageLength, out int count)
		{
			return GetByIDNganhNgheKinhDoanh(null, iDNganhNgheKinhDoanh, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_tb_NganhNgheKinhDoanh index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="iDNganhNgheKinhDoanh"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="Indeco.G24.Entities.NganhNgheKinhDoanh"/> class.</returns>
		public abstract Indeco.G24.Entities.NganhNgheKinhDoanh GetByIDNganhNgheKinhDoanh(TransactionManager transactionManager, System.Guid iDNganhNgheKinhDoanh, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a Indeco.G24.Entities.TList&lt;NganhNgheKinhDoanh&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="Indeco.G24.Entities.TList&lt;NganhNgheKinhDoanh&gt;"/></returns>
		public static Indeco.G24.Entities.TList<NganhNgheKinhDoanh> Fill(IDataReader reader, Indeco.G24.Entities.TList<NganhNgheKinhDoanh> rows, int start, int pageLength)
		{
		// advance to the starting row
		for (int i = 0; i < start; i++)
		{
			if (!reader.Read())
			return rows; // not enough rows, just return
		}
		for (int i = 0; i < pageLength; i++)
		{
			if (!reader.Read())
			break; // we are done
			string key = null;
			
			Indeco.G24.Entities.NganhNgheKinhDoanh c = null;
			if (DataRepository.Provider.UseEntityFactory)
			{
			key = new System.Text.StringBuilder("NganhNgheKinhDoanh")
			.Append("|").Append((reader.IsDBNull(((int)Indeco.G24.Entities.NganhNgheKinhDoanhColumn.IDNganhNgheKinhDoanh - 1))?Guid.NewGuid():(System.Guid)reader[((int)Indeco.G24.Entities.NganhNgheKinhDoanhColumn.IDNganhNgheKinhDoanh - 1)]).ToString()).ToString();
			c = EntityManager.LocateOrCreate<NganhNgheKinhDoanh>(
			key.ToString(), // EntityTrackingKey
			"NganhNgheKinhDoanh",  //Creational Type
			DataRepository.Provider.EntityCreationalFactoryType,  //Factory used to create entity
			DataRepository.Provider.EnableEntityTracking); // Track this entity?
			}
			else
			{
			c = new Indeco.G24.Entities.NganhNgheKinhDoanh();
			}
			
			if (!DataRepository.Provider.EnableEntityTracking ||
			c.EntityState == EntityState.Added ||
			(DataRepository.Provider.EnableEntityTracking &&
			((DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
			(DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.DiscardChanges && (c.EntityState == EntityState.Unchanged ||
								c.EntityState == EntityState.Changed)))))
						{
			c.SuppressEntityEvents = true;
			c.IDNganhNgheKinhDoanh = (System.Guid)reader["ID_NganhNgheKinhDoanh"];
			c.OriginalIDNganhNgheKinhDoanh = c.IDNganhNgheKinhDoanh;
			c.Ten = (System.String)reader["Ten"];
			c.Mota = reader.IsDBNull(reader.GetOrdinal("Mota")) ? null : (System.String)reader["Mota"];
			c.TrangThai = (System.Boolean)reader["TrangThai"];
			c.EntityTrackingKey = key;
			c.AcceptChanges();
			c.SuppressEntityEvents = false;
			}
			rows.Add(c);
		}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="Indeco.G24.Entities.NganhNgheKinhDoanh"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="Indeco.G24.Entities.NganhNgheKinhDoanh"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, Indeco.G24.Entities.NganhNgheKinhDoanh entity)
		{
			if (!reader.Read()) return;
			
			entity.IDNganhNgheKinhDoanh = (System.Guid)reader["ID_NganhNgheKinhDoanh"];
			entity.OriginalIDNganhNgheKinhDoanh = (System.Guid)reader["ID_NganhNgheKinhDoanh"];
			entity.Ten = (System.String)reader["Ten"];
			entity.Mota = reader.IsDBNull(reader.GetOrdinal("Mota")) ? null : (System.String)reader["Mota"];
			entity.TrangThai = (System.Boolean)reader["TrangThai"];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="Indeco.G24.Entities.NganhNgheKinhDoanh"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="Indeco.G24.Entities.NganhNgheKinhDoanh"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, Indeco.G24.Entities.NganhNgheKinhDoanh entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.IDNganhNgheKinhDoanh = (System.Guid)dataRow["ID_NganhNgheKinhDoanh"];
			entity.OriginalIDNganhNgheKinhDoanh = (System.Guid)dataRow["ID_NganhNgheKinhDoanh"];
			entity.Ten = (System.String)dataRow["Ten"];
			entity.Mota = Convert.IsDBNull(dataRow["Mota"]) ? null : (System.String)dataRow["Mota"];
			entity.TrangThai = (System.Boolean)dataRow["TrangThai"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="Indeco.G24.Entities.NganhNgheKinhDoanh"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">Indeco.G24.Entities.NganhNgheKinhDoanh Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		internal override void DeepLoad(TransactionManager transactionManager, Indeco.G24.Entities.NganhNgheKinhDoanh entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByIDNganhNgheKinhDoanh methods when available
			
			#region NhaCungCapNganhNgheKinhDoanhCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<NhaCungCapNganhNgheKinhDoanh>|NhaCungCapNganhNgheKinhDoanhCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'NhaCungCapNganhNgheKinhDoanhCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.NhaCungCapNganhNgheKinhDoanhCollection = DataRepository.NhaCungCapNganhNgheKinhDoanhProvider.GetByIDNganhNgheKinhDoanh(transactionManager, entity.IDNganhNgheKinhDoanh);

				if (deep && entity.NhaCungCapNganhNgheKinhDoanhCollection.Count > 0)
				{
					deepHandles.Add("NhaCungCapNganhNgheKinhDoanhCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<NhaCungCapNganhNgheKinhDoanh>) DataRepository.NhaCungCapNganhNgheKinhDoanhProvider.DeepLoad,
						new object[] { transactionManager, entity.NhaCungCapNganhNgheKinhDoanhCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region NhaCungCapCollection_From_NhaCungCapNganhNgheKinhDoanh
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<NhaCungCap>|NhaCungCapCollection_From_NhaCungCapNganhNgheKinhDoanh", deepLoadType, innerList))
			{
				entity.NhaCungCapCollection_From_NhaCungCapNganhNgheKinhDoanh = DataRepository.NhaCungCapProvider.GetByIDNganhNgheKinhDoanhFromNhaCungCapNganhNgheKinhDoanh(transactionManager, entity.IDNganhNgheKinhDoanh);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'NhaCungCapCollection_From_NhaCungCapNganhNgheKinhDoanh' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.NhaCungCapCollection_From_NhaCungCapNganhNgheKinhDoanh != null)
				{
					deepHandles.Add("NhaCungCapCollection_From_NhaCungCapNganhNgheKinhDoanh",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< NhaCungCap >) DataRepository.NhaCungCapProvider.DeepLoad,
						new object[] { transactionManager, entity.NhaCungCapCollection_From_NhaCungCapNganhNgheKinhDoanh, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the Indeco.G24.Entities.NganhNgheKinhDoanh object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">Indeco.G24.Entities.NganhNgheKinhDoanh instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">Indeco.G24.Entities.NganhNgheKinhDoanh Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		internal override bool DeepSave(TransactionManager transactionManager, Indeco.G24.Entities.NganhNgheKinhDoanh entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<Delegate, object> deepHandles = new Dictionary<Delegate, object>();

			#region NhaCungCapCollection_From_NhaCungCapNganhNgheKinhDoanh>
			if (CanDeepSave(entity.NhaCungCapCollection_From_NhaCungCapNganhNgheKinhDoanh, "List<NhaCungCap>|NhaCungCapCollection_From_NhaCungCapNganhNgheKinhDoanh", deepSaveType, innerList))
			{
				if (entity.NhaCungCapCollection_From_NhaCungCapNganhNgheKinhDoanh.Count > 0 || entity.NhaCungCapCollection_From_NhaCungCapNganhNgheKinhDoanh.DeletedItems.Count > 0)
				{
					DataRepository.NhaCungCapProvider.Save(transactionManager, entity.NhaCungCapCollection_From_NhaCungCapNganhNgheKinhDoanh); 
					deepHandles.Add(
						(DeepSaveHandle<NhaCungCap>) DataRepository.NhaCungCapProvider.DeepSave,
						new object[] { transactionManager, entity.NhaCungCapCollection_From_NhaCungCapNganhNgheKinhDoanh, deepSaveType, childTypes, innerList }
					);
				}
			}
			#endregion 
	
			#region List<NhaCungCapNganhNgheKinhDoanh>
				if (CanDeepSave(entity.NhaCungCapNganhNgheKinhDoanhCollection, "List<NhaCungCapNganhNgheKinhDoanh>|NhaCungCapNganhNgheKinhDoanhCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(NhaCungCapNganhNgheKinhDoanh child in entity.NhaCungCapNganhNgheKinhDoanhCollection)
					{
						if(child.IDNganhNgheKinhDoanhSource != null)
							child.IDNganhNgheKinhDoanh = child.IDNganhNgheKinhDoanhSource.IDNganhNgheKinhDoanh;
						else
							child.IDNganhNgheKinhDoanh = entity.IDNganhNgheKinhDoanh;

						//Handle right table of NhaCungCapCollection_From_NhaCungCapNganhNgheKinhDoanh
						if(child.IDNhaCungCapSource != null)
						{
								child.IDNhaCungCap = child.IDNhaCungCapSource.IDNhaCungCap;

						}

						//Handle right table of NhaCungCapCollection_From_NhaCungCapNganhNgheKinhDoanh
						if(child.IDNganhNgheKinhDoanhSource != null)
						{
								child.IDNganhNgheKinhDoanh = child.IDNganhNgheKinhDoanhSource.IDNganhNgheKinhDoanh;

						}

					}

					if (entity.NhaCungCapNganhNgheKinhDoanhCollection.Count > 0 || entity.NhaCungCapNganhNgheKinhDoanhCollection.DeletedItems.Count > 0)
					{
						DataRepository.NhaCungCapNganhNgheKinhDoanhProvider.Save(transactionManager, entity.NhaCungCapNganhNgheKinhDoanhCollection);
						
						deepHandles.Add(
							(DeepSaveHandle< NhaCungCapNganhNgheKinhDoanh >) DataRepository.NhaCungCapNganhNgheKinhDoanhProvider.DeepSave,
							new object[] { transactionManager, entity.NhaCungCapNganhNgheKinhDoanhCollection, deepSaveType, childTypes, innerList }
						);
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region NganhNgheKinhDoanhChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>Indeco.G24.Entities.NganhNgheKinhDoanh</c>
	///</summary>
	public enum NganhNgheKinhDoanhChildEntityTypes
	{

		///<summary>
		/// Collection of <c>NganhNgheKinhDoanh</c> as OneToMany for NhaCungCapNganhNgheKinhDoanhCollection
		///</summary>
		[ChildEntityType(typeof(TList<NhaCungCapNganhNgheKinhDoanh>))]
		NhaCungCapNganhNgheKinhDoanhCollection,

		///<summary>
		/// Collection of <c>NganhNgheKinhDoanh</c> as ManyToMany for NhaCungCapCollection_From_NhaCungCapNganhNgheKinhDoanh
		///</summary>
		[ChildEntityType(typeof(TList<NhaCungCap>))]
		NhaCungCapCollection_From_NhaCungCapNganhNgheKinhDoanh,
	}
	
	#endregion NganhNgheKinhDoanhChildEntityTypes
	
	#region NganhNgheKinhDoanhFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;NganhNgheKinhDoanhColumn&gt;"/> class
	/// that is used exclusively with a <see cref="NganhNgheKinhDoanh"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class NganhNgheKinhDoanhFilterBuilder : SqlFilterBuilder<NganhNgheKinhDoanhColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the NganhNgheKinhDoanhFilterBuilder class.
		/// </summary>
		public NganhNgheKinhDoanhFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the NganhNgheKinhDoanhFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public NganhNgheKinhDoanhFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the NganhNgheKinhDoanhFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public NganhNgheKinhDoanhFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion NganhNgheKinhDoanhFilterBuilder
	
	#region NganhNgheKinhDoanhParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;NganhNgheKinhDoanhColumn&gt;"/> class
	/// that is used exclusively with a <see cref="NganhNgheKinhDoanh"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class NganhNgheKinhDoanhParameterBuilder : ParameterizedSqlFilterBuilder<NganhNgheKinhDoanhColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the NganhNgheKinhDoanhParameterBuilder class.
		/// </summary>
		public NganhNgheKinhDoanhParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the NganhNgheKinhDoanhParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public NganhNgheKinhDoanhParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the NganhNgheKinhDoanhParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public NganhNgheKinhDoanhParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion NganhNgheKinhDoanhParameterBuilder
} // end namespace
